#pragma once

#include <iostream>
#include <string>
#include <cstring>
#include <cerrno>
#include <cassert>
#include <unistd.h>
#include <memory>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <ctype.h>
#include <fcntl.h>

class Sock
{
private:
    // listen的第二个参数是底层全连接队列的长度,其数值为listen的第二个参数+1
    const static int gbackLog = 10;
public:
    Sock()
    {}

    // 创建套接字
    static int Socket()
    {
        int listenSock = socket(AF_INET, SOCK_STREAM, 0);
        if(listen < 0) exit(2);
        int opt = 1;
        setsockopt(listenSock, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt)); // 复用IP地址和端口号
        return listenSock;
    }

    // 绑定IP地址和端口号
    static void Bind(int sock, uint16_t port, std::string ip = "0.0.0.0")
    {
        struct sockaddr_in local;
        memset(&local, 0, sizeof local);
        local.sin_family = AF_INET;
        local.sin_port = htons(port);
        inet_pton(AF_INET, ip.c_str(), &local.sin_addr);
        if(bind(sock, (struct sockaddr*)&local, sizeof(local)) < 0) exit(3);
    }

    static void Listen(int sock)
    {
        if(listen(sock, gbackLog) < 0)
            exit(4);
    }

    // 一般经验
    // const std::string &: 输入型参数
    // std::string *: 输出型参数    
    // std::string &: 输入输出型参数
    // 获取连接
    static int Accept(int listenSock, std::string* ip, uint16_t* port)
    {
        struct sockaddr_in src;
        socklen_t len = sizeof(src);
        int serviceSock = accept(listenSock, (struct sockaddr*)&src, &len);
        if(serviceSock < 0) return -1;
        if(port) *port = ntohs(src.sin_port);
        if(ip) *ip = inet_ntoa(src.sin_addr);
        return serviceSock;
    }

    // 发起连接
    static bool Connect(int sock, const std::string& serverIp, uint16_t& serverPort)
    {
        struct sockaddr_in server;
        memset(&server, 0, sizeof server);
        server.sin_family = AF_INET;
        server.sin_port = htons(serverPort);
        server.sin_addr.s_addr = inet_addr(serverIp.c_str());

        if(connect(sock, (struct sockaddr*)&server, sizeof server) == 0) return true;
        else return false;
    }

    // 将sock设置为非阻塞
    static bool SetNonBlock(int sock)
    {
        int fl = fcntl(sock, F_GETFL);
        if(fl < 0)  return false;
        fcntl(sock, F_SETFL, fl | O_NONBLOCK);
        return true;
    }

    ~Sock()
    {}
};